//index.js
//获取应用实例
const app = getApp()
const api = require('../../Datas/api.js');
Page({
  data: {
    classNames: [],
    classIds:[],
    index:0,


    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  formSubmit: function (e) {
    let values = e.detail.value;
    let data = [];
    data['classId'] = this.data.classIds[values.classIndex];
    data['id'] = this.data.userInfo.id;
    console.log(data)
    
    let param = {
      API_URL: app.globalData.apiUrl + 'videoData/updateWechatUserInfo',
      method: "POST",
      data: data
    };

    api.result(param).then(data => {
      let datas = data.data.data;
      console.log(datas)

      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })

    }).catch(e => {
      console.log(e)
      // this.setData({
      //   loadtxt: '数据加载异常',
      //   loading: false
      // })

    })


  },

  onLoad: function () {
    var $this = this;
    app.saveWechatUserInfo(app, function (userInfo){
      $this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
    console.log($this.data.classIds)
    let classIds = $this.data.classIds;
    for(let i=0;i<classIds.length;i++){
      if (classIds[i] == userInfo.classId){
        $this.setData({
          index:i
        });
        break;
      }
    }

        
    })

    $this.getClassList();

  },
  getClassList:function(){
    var $this = this;
    var classNames = [];
    var classIds=[];
    let param = {
      API_URL: app.globalData.apiUrl + '/videoData/getClassList',
    };

    api.result(param).then(data => {
      let datas = data.data.data;
      datas.map(function(item,i){
        classNames[i] = item.name;
        classIds[i] = item.classId;
      });
      console.log(classNames)
      console.log(classIds)
      $this.setData({
        classNames: classNames,
        classIds: classIds,
      })

    }).catch(e => {
      console.log(e)
      // this.setData({
      //   loadtxt: '数据加载异常',
      //   loading: false
      // })

    })
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
